package com.lst.servlet;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.FocusReqPara;
import com.lst.entity.response.FocusResPara;
import com.lst.model.MessageReadLog;
import com.lst.model.MstMessage;
import com.lst.service.DynamicService;
import com.lst.service.FocusService;
import com.lst.service.MessageReadLogService;
import com.lst.service.MstMessageService;
import com.lst.utils.RequestUtils;


/**
 * @ClassName: FocusTotalServlet
 * @Description: 关注统计接口
 * 
 */
public class FocusTotalServlet extends BaseServlet {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private FocusService focusService = ctx.getBean(FocusService.class);
	
	private DynamicService dynamicService = ctx.getBean(DynamicService.class);
	
    private MstMessageService mstMessageService = ctx.getBean(MstMessageService.class);
    
    private MessageReadLogService messageReadLogService = ctx.getBean(MessageReadLogService.class);
	
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		FocusResPara resPara = new FocusResPara();

		try {
			FocusReqPara reqPara = getReqPara(request);
		
		if(isCheckField(reqPara,resPara)){
			Integer userid = Integer.valueOf(reqPara.getUserid());
			
			//关注数量
			int totalNum=focusService.countFocusTotal(userid);
			//粉丝数量
			int totalFansNum=focusService.countFansTotal(userid);
			//动态数量
			int dynamicTotalNum=dynamicService.countTotal(userid);
			
			//未读通知数
			Map<String,Object> map =new HashMap<String, Object>();
			map.put("userid",userid);
			map.put("reserved7",false);
			map.put("enabled", true);
			map.put("isDeleted", false);
			int messageNum1=mstMessageService.countByMap(map);
			
			Map<String, Object> cmMap = new HashMap<String, Object>();

			cmMap.put("enabled", true);
			cmMap.put("isDeleted", false);
			cmMap.put("receiveid", userid);
			cmMap.put("orderByClause", "mm.createdate desc");

			PageBounds pageBounds = new PageBounds(1, 100);

			// 查询官方通知列表
			List<MstMessage> mmlist = mstMessageService.queryByList2(cmMap,pageBounds);
			
			int messageNum2=0;
			if(mmlist!=null&&mmlist.size()>0){
				for(MstMessage mm:mmlist){
				//判断消息是否读
				Map<String, Object> map2 = new HashMap<String, Object>();
				map2.put("messageId", mm.getId());
				map2.put("createUser", userid);
				MessageReadLog messageReadLog=messageReadLogService.selectByMap(map2);
				if(messageReadLog!=null){
					
				}else{
					messageNum2++;
				}
			  }
			}
			int messageNum=messageNum1+messageNum2;
			resPara.setDynamicTotalNum(dynamicTotalNum);
			resPara.setTotalNum(totalNum);
			resPara.setTotalFansNum(totalFansNum);
			resPara.setMessageNum(messageNum);
			resPara.setCode(CommCode.M_Y000000);
			resPara.setMessage(CommCode.M_Y000001);
		  }
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}
		
		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
	//		logger.info("FocusTotalServlet jsonResult:" + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

	//		logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public FocusReqPara getReqPara(HttpServletRequest request) {

		FocusReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new FocusReqPara());

			reqPara = (FocusReqPara) baseRequest.clone();

			String userid = request.getParameter("userid");
			
		//	logger.info("reqPara userid:" + userid);
	
	        reqPara.setUserid(userid);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		FocusReqPara cdReqPara = (FocusReqPara) reqPara;
		BaseResponse cdResPara = (BaseResponse) resPara;

       if (StringUtils.isEmpty(cdReqPara.getUserid())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000002);
		} else {
			flag = true;
		}

		return  flag;
	}

}
